package cdworks.lifecounter;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.support.v4.app.NavUtils;
import android.support.v4.widget.CursorAdapter;
import android.support.v4.widget.SimpleCursorAdapter;
import android.annotation.TargetApi;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.database.Cursor;
import android.os.Build;

public class ActivityCheck extends Activity {
	
	private ListView listview;
	private LCDbManager manager;
	private String [] columns = new String[] {LCContract.LCEntry._ID, LCContract.LCEntry.COLUMN_NAME_TYPE, LCContract.LCEntry.COLUMN_NAME_AMOUNT, 
        LCContract.LCEntry.COLUMN_NAME_TIME, LCContract.LCEntry.COLUMN_NAME_SIGN};
	private SimpleCursorAdapter la;
	private Cursor cursor;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_check);
		// Show the Up button in the action bar.
		setupActionBar();
		
		manager = new LCDbManager(this);
		
		listview=(ListView)findViewById(R.id.amount_view);
		
		cursor = manager.getDb().query(LCContract.LCEntry.TABLE_NAME, columns, null, null, null, null, null);
			
		//startManagingCursor(cursor);
		
		la = new SimpleCursorAdapter(this, R.layout.listviewlayout, cursor,
				columns,
				new int[]{R.id.content_id, R.id.content_type, R.id.content_amount, R.id.content_time, R.id.content_inout}, CursorAdapter.NO_SELECTION);
		
		
		//count the total amount
		cursor.moveToFirst();
		int amount = 0;
		String sign;
		while (!cursor.isAfterLast()) {
			sign = cursor.getString(LCContract.COLUMN_SIGN_INDEX);
			if (sign.equalsIgnoreCase("+"))
				amount = amount+cursor.getInt(LCContract.COLUMN_AMOUNT_INDEX);
			else
				amount = amount-cursor.getInt(LCContract.COLUMN_AMOUNT_INDEX);
			cursor.moveToNext();
		}
		//set total amount
		TextView tv = (TextView)findViewById(R.id.total_amount);
		tv.setText(amount+"");
		
		
		listview.setAdapter(la);
		
		listview.setOnItemLongClickListener(new OnItemLongClickListener(){
			
			
			@Override
			public boolean onItemLongClick(AdapterView<?> parent, View view,
					int position, long id) {
				showOption((int)id, position);
				return true;
			}
				
		});
		
	}
	
	
	private void showOption(final int id, final int pos){
		AlertDialog.Builder builder = new Builder(this);
		builder.setTitle("Options:");
		builder.setPositiveButton("Edit", new OnClickListener(){

			@Override
			public void onClick(DialogInterface arg0, int arg1) {
				// TODO Auto-generated method stub
				
			}
			
		});
		builder.setNeutralButton("Delete", new OnClickListener(){

			@SuppressWarnings("deprecation")
			@Override
			public void onClick(DialogInterface arg0, int arg1) {
				manager.getDb().delete(LCContract.LCEntry.TABLE_NAME,LCContract.LCEntry._ID+"='"+id+"';", null);
				la.notifyDataSetChanged();
				listview.invalidate();
				cursor.requery();
				
			}
			
		});
		builder.setNegativeButton("Cancel", new OnClickListener(){

			@Override
			public void onClick(DialogInterface dialog, int arg1) {
				dialog.dismiss();
				
			}
			
		});
		builder.create().show();
		
		
	}

	/**
	 * Set up the {@link android.app.ActionBar}, if the API is available.
	 */
	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	private void setupActionBar() {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
			getActionBar().setDisplayHomeAsUpEnabled(true);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.check, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			// This ID represents the Home or Up button. In the case of this
			// activity, the Up button is shown. Use NavUtils to allow users
			// to navigate up one level in the application structure. For
			// more details, see the Navigation pattern on Android Design:
			//
			// http://developer.android.com/design/patterns/navigation.html#up-vs-back
			//
			NavUtils.navigateUpFromSameTask(this);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

}
